// pages/user/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '0',
            'title': '海螺号工蜂',
            'color': true,
            'class': false
        },
        pageHeight: app.globalData.pageHeight, //手机屏幕高度
        dateSelector: app.MultipleSelectDate,
        imagesURL: app.imagesURL,
        days: 30,
        date: "详情",
        waterType: 1, //1 罚金
        bindShop: {
            sumSalesmanIncomeMoney: 0,
            dealerId: 0
        },
        scrollTop: 0,
        pitchOn: 0,
        pageNum: 1,
        dialogOne: false,
        dialogTwo: false,
        dialogThree: false,
        slide: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        wx.getStorage({
                key: 'chatInfo',
                success(res) {
                    console.log(res)
                    that.setData({
                        chatName: res.data.userInfo.nickName,
                        chatHead: res.data.userInfo.avatarUrl
                    })
                }
            }),
            wx.getStorage({
                key: 'userInfo',
                success(res) {
                    that.setData({
                        salesmanId: res.data.id,
                        userInfo: res.data,
                        navH: app.globalData.navHeight
                    })
                    that.creditMargin()
                    that.formatTime(30)
                    that.income()
                    that.incomeIncomeFlowData(30)
                }
            })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading() //在标题栏中显示加载图标
        this.creditMargin()
        this.formatTime(30)
        this.income()
        this.incomeIncomeFlowData(30)
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //得到数据后停止下拉刷新
    },


    /* 获取信用保证金内容 */
    creditMargin(e) {
        var that = this;
        // /bdservice/bdb/WorkerBee/creditMargin
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'creditMargin',
            q: {
                salesmanId: that.data.salesmanId
            }
        }), (res) => {}, res => {
            that.setData({
                guarantee: res.data
            })
        })
    },

    /* 获取流水数据 扣款、充值、回款流水 */
    flowData() {
        var that = this,
            pageNum = 1,
            list = [];
        var data = {
            "endDate": that.data.endTime,
            "pageNum": that.data.pageNum,
            "pageSize": 10,
            "salesmanId": that.data.salesmanId,
            "startDate": that.data.startTime,
            "type": that.data.waterType
        }
        // /bdservice/bdb/WorkerBee/flowData
        app.basePost(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'flowData'
        }), data, res => {
            pageNum = res.data.pageNum;
            if (pageNum == 1) {
                console.log(1)
                console.log(res.data.flowDataList)
                list = res.data.flowDataList;
            } else {
                console.log(2)
                list = app.SplitArray(res.data.flowDataList ? res.data.flowDataList : [], that.data.blotter) //分页合并数组
            }
            that.setData({
                blotter: list,
                pageNum: pageNum
            })
        }, (res) => {})
    },

    /* 我的收益 */
    income(e) {
        var that = this;
        // /bdb/WorkerBee/income
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'income',
            q: {
                salesmanId: that.data.salesmanId
            }
        }), (res) => {}, res => {
            that.setData({
                income: res.data
            })
        })
    },

    /* 获取流收益流水 */
    incomeIncomeFlowData() {
        var that = this,
            pageNum = 1,
            list = [];
        var data = {
            "endDate": that.data.endTime,
            "pageNum": that.data.pageNum,
            "orderNo": '',
            "pageSize": 10,
            "salesmanId": that.data.salesmanId,
            "startDate": that.data.startTime,
            "type": 0
        }
        // /bdb/WorkerBee/incomeIncomeFlowData
        app.basePost(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'incomeIncomeFlowData'
        }), data, res => {
            pageNum = res.data.pageNum;
            if (pageNum == 1) {
                list = res.data.list;
                console.log(pageNum)
            } else {
                list = app.SplitArray(res.data.list ? res.data.list : [], that.data.returned) //分页合并数组
            }
            that.setData({
                returned: list
            })
        }, (res) => {
            console.log(res)
        })
    },
    /* 扣款详情 */
    withhold(e) {
        var that = this;
        // /bdservice/bdb/WorkerBee/DebitDetails
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'DebitDetails',
            q: {
                id: e.currentTarget.dataset.id
            }
        }), (res) => {}, res => {
            console.log(res)
            that.setData({
                dialogList: res.data,
                dialogThree: true,
                slide: true
            })
        })
    },
    /* 充值详情 */
    fine(e) {
        var that = this;
        // /bdservice/bdb/WorkerBee/RechargeDetails
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'RechargeDetails',
            q: {
                id: e.currentTarget.dataset.id
            }
        }), (res) => {}, res => {
            that.setData({
                recharge: res.data,
                dialogOne: true
            })
        })
    },
    /* 回款详情 */
    refund(e) {
        var that = this;
        // /bdservice/bdb/WorkerBee/PaymentDetails
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'PaymentDetails',
            q: {
                id: e.currentTarget.dataset.id
            }
        }), (res) => {
            console.log(res)
        }, res => {
            console.log(res)
            that.setData({
                payment: res.data,
                dialogTwo: true
            })
        })
    },
    // 发送给朋友
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
        }
        return {
            title: '海螺号工蜂',
            path: 'pages/logs/logs'
        }
    },

    // 滚动到底部加载更多
    waterLower() {
        var pageNum = Number(this.data.pageNum) + 1;
        this.setData({
            pageNum: pageNum,
        })
        if (this.data.pitchOn == 0) {
            this.flowData()
        } else {
            this.incomeIncomeFlowData()
        }
    },

    handleWater(e) {
        var itemIndex = e.currentTarget.dataset['index']
        this.setData({
            waterType: itemIndex,
            pageNum: 1,
            blotter: []
        })
        this.flowData()
    },
    tabSelect: function (e) {
        let index = e.currentTarget.dataset.current
        this.setData({
            pitchOn: index,
            pageNum: 1,
            scrollTop: 0
        })
    },

    handleDate: function (e) {
        let index = e.currentTarget.dataset.item;
        this.setData({
            days: index,
            blotter: [],
            pageNum: 1
        })
        this.formatTime(index)
    },

    //时间格式化
    formatTime: function (index, date) {
        var date = new Date().getTime();
        var onDate = new Date(date + 86400000);
        var preDate = new Date(date - (index * 86400000)); //前n天
        this.setData({
            startTime: app.formatdata('yyyy-MM-dd 00:00:00', index != 0 ? preDate : new Date()),
            endTime: app.formatdata('yyyy-MM-dd 00:00:00', onDate)
        })
        this.flowData()
        this.incomeIncomeFlowData()
    },

    goMoneyPay: function () {
        wx.navigateTo({
            url: '/pages/subrogation/moneyPay/index',
        })
    },

    goDebt: function () {
        wx.navigateTo({
            url: '/pages/subrogation/onAccount/index',
        })
    },

    goPayment: function (e) {
        console.log(e)
        wx.navigateTo({
            url: '/pages/subrogation/marginFlow/index?index=' + e.currentTarget.dataset.index,
        })
    },

    goMarginFlow: function () {
        wx.navigateTo({
            url: '/pages/subrogation/marginFlow/index',
        })
    },

    goEarnings: function () {
        wx.navigateTo({
            url: '/pages/subrogation/earnings/index',
        })
    },

    getUserProfile() {
        wx.getUserProfile({
            desc: '获取微信头像和昵称',
            success: (res) => {
                wx.setStorageSync('chatInfo', res)
                this.setData({
                    chatName: res.userInfo.nickName,
                    chatHead: res.userInfo.avatarUrl
                });
            },
            fail: res => {
                wx.showToast({
                    icon: 'none',
                    title: '获取用户信息失败'
                })
                console.log("获取用户信息失败", res)
            }
        })
    },
    //充值成功
    dialogOne() {
        this.setData({
            dialogOne: this.data.dialogOne ? false : true
        })
    },
    //回款详情
    dialogTwo() {
        this.setData({
            dialogTwo: this.data.dialogTwo ? false : true
        })
    },
    //扣款详情
    dialogThree() {
        this.setData({
            dialogThree: this.data.dialogThree ? false : true
        })
    },
    slide(){
        this.setData({
            slide: false
        })
    }
})